package com.tramy.cn.push.sender;

import com.tramy.cn.push.common.*;
import com.tramy.hpush.server.Sender;
import com.tramy.hpush.server.data.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HUAWEISender implements PushSender {

    public static final Log logger = LogFactory.getLog(HUAWEISender.class);

    private final Sender sender;

    private final ErrorListener errorListener;

    private final ExecutorService executorService;

    public HUAWEISender(HUAWEIInfo info) {
        sender = new Sender(info.getAppId(),
                info.getAppSecret());

        errorListener = info.getErrorListener();
        executorService = Executors.newFixedThreadPool(
                info.getPoolSize() == null ? 10 : info.getPoolSize());
    }

    @Override
    public PushRes sendData(List<Data> dataList) {
        if (dataList == null || dataList.isEmpty()) {
            logger.info("【CNPUSH SDK - HUAWEI】目标平台没有要进行的推送");
            return PushRes.ok();
        }

        List<Message> messages = getMessages(dataList);

        for (Message message : messages) {
            executorService.execute(() -> {
                try {
                    PushResponse response = sender.send(message);
                    if (!response.getCode().equals(PushResponse.SUCCESS)) {
                        String errorInfo = message.getToken() +
                                "," +
                                response.getCode() +
                                "," +
                                response.getMsg();

                        if (errorListener != null) {
                            errorListener.onReceiveInfo(errorInfo);
                        }
                    }
                }catch (IOException e) {
                    String errorInfo = message.getToken() +
                            "," +
                            e.toString();

                    if (errorListener != null) {
                        errorListener.onReceiveInfo(errorInfo);
                    }
                }
            });
        }

        return PushRes.ok();
    }

    private List<Message> getMessages(List<Data> dataList) {
        List<Message> messages = new LinkedList<>();
        for (Data data : dataList) {
            ClickAction clickAction = ClickAction.builder()
                    .setType(3)
                    .build();
            AndroidNotification.Builder notificationBuilder = AndroidNotification.builder()
                    .setTitle(data.getTitle())
                    .setBody(data.getContent())
                    .setClickAction(clickAction);

            if (data.getGrantHms() && data.getLevel() == LEVEL.HIGH) {
                notificationBuilder.setImportance("NORMAL");
            }
            AndroidConfig android = AndroidConfig.builder()
                    .setNotification(notificationBuilder.build())
                    .build();
            Message message = Message.builder()
                    .setAndroidConfig(android)
                    .addToken(data.getToken())
                    .build();
            messages.add(message);
        }
        return messages;
    }

}
